package com.zmn.oms.admin.controller.conf.attachment;

import com.google.common.collect.Lists;
import com.zmn.common.dto2.data.LayuiTableDO;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.oms.admin.controller.OmsBaseController;
import com.zmn.oms.business.interfaces.conf.attachment.ConfOrderAttachmentBService;
import com.zmn.oms.common.dto.ResultDTO;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.model.entity.conf.attachment.ConfOrderAttachment;
import com.zmn.oms.model.entity.conf.attachment.ConfOrderAttachmentQuery;
import com.zmn.oms.model.vo.conf.attachment.ConfOrderAttachmentVO;
import com.zmn.oms.services.interfaces.conf.attachment.ConfOrderAttachmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Objects;

/**
 * 类描述：附件配置
 *
 * @author ligang
 * @date 2021/09/25 11:03
 */
public class ConfOrderAttachmentController extends OmsBaseController {

    @Autowired
    protected ConfOrderAttachmentService confOrderAttachmentService;
    @Autowired
    protected ConfOrderAttachmentBService confOrderAttachmentBService;

    /**
     * 查询列表 ajax
     */
    protected ResultDTO ajaxList(ConfOrderAttachmentQuery query) {
        if (StringUtil.isNotBlank(query.getCategIds())) {
            query.setCategIdList(Lists.newArrayList(query.getCategIds().split(",")));
        }
        if (StringUtil.isNotBlank(query.getChannelIds())) {
            query.setChannelIdList(Lists.newArrayList(query.getChannelIds().split(",")));
        }
        if (StringUtil.isNotBlank(query.getBizLineIds())) {
            query.setBizLineIdList(Lists.newArrayList(query.getBizLineIds().split(",")));
        }
        if (StringUtil.isNotBlank(query.getServCategIds())) {
            query.setServCategIdList(Lists.newArrayList(query.getServCategIds().split(",")));
        }
        List<ConfOrderAttachmentVO> confList = confOrderAttachmentBService.listPageByQuery(query);
        LayuiTableDO<ConfOrderAttachmentVO> data = new LayuiTableDO();
        data.setCount(query.getTotalCount());
        data.setData(confList);
        return ResultDTO.success(data);
    }

    /**
     * 查看/编辑
     *
     * @param mv
     * @param id
     * @return
     */
    protected ModelAndView detail(ModelAndView mv, Integer id) {
        ConfOrderAttachmentVO conf;
        if (Objects.nonNull(id)) {
            conf = confOrderAttachmentService.findConfById(id);
        } else {
            conf = ConfOrderAttachmentVO.builder().build();
        }
        mv.addObject("conf", conf);
        return mv;
    }

    /**
     * 保存
     *
     * @param conf
     * @return
     */
    public ResultDTO save(ConfOrderAttachment conf) {
        try {
            if (Objects.nonNull(conf.getId())) {
                conf.setUpdater(super.getCurrentStaff().getRealName());
                confOrderAttachmentBService.modifyConfById(conf);
                return ResultDTO.success();
            } else {
                conf.setCreater(super.getCurrentStaff().getRealName());
                Integer id = confOrderAttachmentBService.addConf(conf);
                return ResultDTO.success(id);
            }
        } catch (OmsBaseException e) {
            e.printStackTrace();
            return ResultDTO.fail(e.getMessage());
        }
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    protected ResultDTO delete(Integer id) {
        try {
            confOrderAttachmentService.deleteById(id);
            return ResultDTO.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDTO.fail(e.getMessage());
        }
    }
}
